package userInterface;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import org.quickconnect.QuickConnect;

import userInterface.StandardTableModel;


public class ChatUserPanel extends JPanel {
	JTable table;
	Vector list;
	StandardTableModel tableModel;
	
	public ChatUserPanel() {
		super(new BorderLayout());
		
		list = new Vector();
		Vector columns = new Vector();
		columns.add("User");
		columns.add("Role");

		tableModel = new StandardTableModel(list, columns);
		table = new JTable(tableModel);
		
		JButton banButton = new JButton("Ban");
		banButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ArrayList params = new ArrayList();
				HashMap paramsMap = new HashMap();
				paramsMap.put("userToBan",table.getModel().getValueAt(table.getSelectedRow(), 0));
				paramsMap.put("sessionId", ((ChatSessionPanel)MainFrame.mainFrame.getTabs().getSelectedComponent()).getSession().getSessionId());
				params.add(paramsMap);
				QuickConnect.handleRequest("banUser", params);
			}
		});
		JButton modButton = new JButton("Make Moderator");
		modButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ArrayList params = new ArrayList();
				HashMap paramsMap = new HashMap();
				paramsMap.put("userName", table.getModel().getValueAt(table.getSelectedRow(), 0));
				paramsMap.put("sessionId", ((ChatSessionPanel)MainFrame.mainFrame.getTabs().getSelectedComponent()).getSession().getSessionId());
				params.add(paramsMap);
				QuickConnect.handleRequest("makeModerator", params);
			}
		});
		
		JPanel buttonPanel = new JPanel();
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		buttonPanel.add(banButton);
		buttonPanel.add(modButton);

		//Create the list and put it in a scroll pane.
		table.setSelectionMode(
				ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		//        list.setSelectedIndex(0);
		JScrollPane listScrollPane = new JScrollPane(table);
		//Add to chat session 
		add(listScrollPane);
		this.add(buttonPanel);

		listScrollPane.setOpaque(false);
	}
	
	public void setCurrentUserList(Vector list){
		int row = table.getSelectedRow();
		String selectedUser = null;
		if(row > -1)
		{
			selectedUser = (String)table.getModel().getValueAt(row, 0);
		}
		((StandardTableModel)table.getModel()).setItems(list);
		updateTableSelection(selectedUser);
	}
	
	public JTable getTable()
	{
		return table;
	}
	
	private void updateTableSelection(String selectedUser)
	{
		if(selectedUser != null)
		{
			for(int i = 0; i != table.getModel().getRowCount(); i++)
			{
				if(table.getModel().getValueAt(i, 0).equals(selectedUser))
				{
					table.setRowSelectionInterval(i, i);
					break;
				}
			}
		}
	}
	
	public void addUser(String[] user)
	{
		tableModel.getItems().addElement(user);
		tableModel.sortItems();
		tableModel.fireTableDataChanged();
		updateTableSelection(user[0]);
	}
	
	public void removeUser(String[] user)
	{
		String selectedUser = getSelectedUser();
		for(int i = 0; i != tableModel.getItems().size(); i++)
		{
			if(tableModel.getItems().get(i)[0].equals(user[0]))
			{
				tableModel.getItems().remove(i);
				break;
			}
		}
		tableModel.fireTableDataChanged();
		updateTableSelection(selectedUser);
	}
	
	public void updateRole(String[] user)
	{
		for(int i = 0; i != tableModel.getItems().size(); i++)
		{
			if(tableModel.getItems().get(i)[0].equals(user[0]))
			{
				tableModel.getItems().get(i)[1] = user[1];
			}
		}
		tableModel.fireTableDataChanged();
		updateTableSelection(user[0]);
	}
	
	public String getSelectedUser()
	{
		if(table.getSelectedRow() < 0)
		{
			return null;
		}
		return((String)tableModel.getValueAt(table.getSelectedRow(), 0));
	}
}